<script setup>
import axios from 'axios';
import Add from '@/components/app/MenuAdd.vue';
import { onMounted, reactive, ref } from 'vue';
import { showMsg } from '@/components/Util';
let optype = ref('new');
let title = ref('添加菜单');
let list = ref([]);
function getData(){
    axios.get('/api/menu_list').then(res =>{
        console.log(res);
        if(res.data.status==200){
            list.value = res.data.data;
        }else{
            showMsg("获取数据失败",'error',2000);
        }
    })
}

function newMenu(){
    addshow.value=false;
    getData();
}
function del(id){
    axios.delete('/api/menu_delete/'+id).then(res=>{
        console.log(res);
        if(res.data.status==200){
            getData();
        }else{
            showMsg(res.data.msg,'error',2000);
        }
    })
}
function edit(id){
    //显示表单窗口
    //显示数据到窗口
   addshow.value = true;
   //list [{},{}]
   let obj = list.value.filter(item=>item.id==id);
   console.log(obj);
   menu.value = obj[0];
   optype.value = 'edit';
   title.value="修改菜单"

}
let menu = ref({title:'',icon:'',pid:'',auth:'',mobile:'',path:''})
let addshow=ref(false);
onMounted(() =>{
    getData();
})
function news(){
    addshow.value=true;
    title.value="添加菜单";
}
</script>
<template >
    <Add :title="title" :type="optype" :menu="menu" v-show="addshow" @submit="newMenu" @close="addshow=false" ></Add>
    <div class="page">
        <div class="tool">
            <button @click="news">添加</button>
        </div>
        <ul class="list">
            <div class="list1">
                <span>菜单名称</span>
                <span>图表</span>
                <span>上级菜单</span>
                <span>角色</span>
                <span>路由</span>
                <span>按钮</span>
            </div>
            <li class="item" v-for="item in list" :key="item.id">
                <span class="it">{{ item.title }}</span>
                <span>{{ item.icon }}</span>
                <span>{{ item.pid }}</span>
                <span>{{ item.auth }}</span>
                <span>{{ item.path }}</span>
                <span>
                    <button @click="del(item.id)">删除</button>
                    <button @click="edit(item.id)">修改</button>
                </span>
            </li>
        </ul>
    </div>
</template>
<style scoped>
.list{
    margin-bottom: 20px;
    padding: 0;
    box-sizing: border-box;
    height: 200px;
}
.list1{
    height: 50px;
    background-color: rgb(255, 80, 0);
    display: grid;
    grid-template-columns: repeat(6,1fr);
    list-style: none;
}
.list1>span{
    display: flex;
    justify-content: center;
    align-items: center;
}
.list>li{
    display: grid;
    grid-template-columns:repeat(6,1fr);
}
.item>span{
    height: 50px;
    display: flex;
    justify-content: center;
    /* text-align: center; */
    padding-top: 30px;
}
.item button{
    width: 50px;
    height: 25px;
}
span{
    border: rgb(57, 62, 74) 1px solid;
}
.it{
    display: flex; 
}
</style>